//#include <iostream.h>
//#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include <stdbool.h>

int main() /* No parameters */
{
    //  printf("Hallo Welt!\n");

    int i =1, j=0, k=0, d=0;
    int g;
    int bits[8]= {0,0,0,0,0,0,0,0};
    double delta;
    char nextchar;
    int alarm=0;

    // catch server's output
    do g=getchar();
    while(g!=46); /* proceed till the first "." is read */

    for(;;){ /* loop over dots, measure time */
        struct timeval tim;
        gettimeofday(&tim, NULL);
        double t1=tim.tv_sec+(tim.tv_usec/1000000.0);
        getchar();
        gettimeofday(&tim, NULL);
        double t2=tim.tv_sec+(tim.tv_usec/1000000.0);
        delta=t2-t1;
        //printf("%.6lf seconds elapsed\n", t2-t1);
        /* Set bits to zero or one, depending on delay */
        if (delta>0.0005) bits[i]=1; // TODO flexible threashold
            else bits[i]=0;
        i++;

        // if 8 bits complete, reconstruct character, print
        if (i==8){
            nextchar=64*bits[1]+32*bits[2]+16*bits[3]+8*bits[4]+4*bits[5]+2*bits[6]+bits[7];

            if(nextchar==0){ /* end of text alarm */
                if(alarm==1){printf("\n\n"); return 0;}
                alarm++;
            }
            //printf("%c  hat Nummer %d\n",nextchar,nextchar);
            printf("%c",nextchar);
            for (j=0;j<=7;j++) bits[j]=0;
            i=0;
        } /* End printing of next character */
    } /* end looping over dots */
} /* end function main */
